// pages/voucherRecive/voucherRecive.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentCouId:'',
    userCouId:'',
    itemArry:'',
    phone:'',
    sale:0,
    addresslist:[],
    background1:"url('https://sx-manage.oss-cn-hangzhou.aliyuncs.com/pic/radio-2.jpg') no-repeat 0rpx 4rpx",
    background2:"url('https://sx-manage.oss-cn-hangzhou.aliyuncs.com/pic/radio-1.jpg') no-repeat 0rpx 4rpx",
    currentId:'',
    currentAddress:'',
    consigneePhone:'',
    consigneeName:'',
  },
  //添加地址
  addAdress:function(){
    wx.navigateTo({
      url: '/pages/addressAdd/addressAdd'
    })
  },
  //点击地址选中
  checkAddress:function(e){
    let arrry=e.currentTarget.dataset.id.split(',');
    this.setData({
      currentId:arrry[0],
      consigneeName:arrry[1],
      consigneePhone:arrry[2],
      currentAddress:arrry[3],
    })
    console.log(this.data.currentId);
    console.log(this.data.currentAddress);
    console.log(this.data.consigneeName);
    console.log(this.data.consigneePhone);
  },
  //领取
  ok:function(){
    let addressArry={
      currentId:this.data.currentId,
      consigneeName:this.data.consigneeName,
      consigneePhone:this.data.consigneePhone,
      currentAddress:this.data.currentAddress,
    }
    if(this.data.sale==1){
      wx.redirectTo({
        url: '/pages/settlementSale/settlementSale?item='+this.data.itemArry+'&address='+JSON.stringify(addressArry)+'&currentCouId='+this.data.currentCouId+'&userCouId='+this.data.userCouId+'&typeId='+this.data.typeId
      })
    }else{
      wx.redirectTo({
        url: '/pages/settlement/settlement?item='+this.data.itemArry+'&address='+JSON.stringify(addressArry)+'&currentCouId='+this.data.currentCouId
      })
    }
    
  },
  //查询地址列表
  getAdressList: function () {
    console.log(this.data.uid);
    let _this=this;
    console.log(12233);
    wx.request({
      url:app.globalData.onlineUrl+"/addressNew/list",
      header:{'content-type': 'application/json'},
      method:"POST",
      data:{phone:_this.data.phone},
      dataType:"json",
      complete:function(result) {
        console.log(result.data.data);
        let currentId=-1;
        let currentAddress='';
        let consigneePhone='';
        let consigneeName='';
        if(result.data.data.length!=0){
          currentId=result.data.data[0].id;
          currentAddress=result.data.data[0].province+''+result.data.data[0].city+''+result.data.data[0].county+''+result.data.data[0].detailAddress;
          consigneePhone=result.data.data[0].phone;
          consigneeName=result.data.data[0].name;
        }
          _this.setData({
            addresslist:result.data.data,
            currentId:currentId,
            currentAddress:currentAddress,
            consigneePhone:consigneePhone,
            consigneeName:consigneeName,
            btnDisable:currentId==-1?true:false
          })
          console.log(6666,_this.data.currentId);
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(9988,options.addressId);
    console.log(9988,options.currentCouId);
    console.log(9988,options.sale);
    if(options.sale){
      this.setData({
        sale:options.sale
      })
    }else{
      this.setData({
        sale:0
      })
    }
    if(options.currentCouId){
      this.setData({
        currentCouId:options.currentCouId,
        userCouId:options.userCouId,
        typeId:options.typeId,
      })
    }
    if(options.addressId){
      this.setData({
        currentId:options.addressId
      })
    }
    this.setData({
      itemArry:options.item
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      phone:wx.getStorageSync('phone')
    })
    this.getAdressList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    console.log(12242);
    return {
      title: '分享标题',
      path: '/pages/index',
      // imageUrl: '/images/share.png'
    }
  }
})